<?php
namespace Pub\tool;
class Develop
{
    const APPID = 'wx5e83cd24c79593d0';
    const APPSECRET = 'ab24eed6e524946107b2af7c34270583';

    public function script()
    {
        global $_W;
        $_data = array(
            'timestamp'=>TIMESTAMP,
            'noncestr'=>Tool::createCode(),
            'jsapi_ticket'=>$this->ticket(),
            'url'=>$_W['siteurl']
        );
        $_data['signature'] = $this->sign($_data);
        $_script = "<script src='//res.wx.qq.com/open/js/jweixin-1.6.0.js'></script>";
        $_script .= "<script>
                wx.config({
                   debug:false,
                   appId:'".self::APPID."',
                   timestamp:'".$_data['timestamp']."',
                   nonceStr:'".$_data['noncestr']."',
                   signature:'".$_data['signature']."',
                   jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','updateAppMessageShareData','updateTimelineShareData','chooseWXPay','hideMenuItems','getLocation']
                });
            </script>";
        return $_script;
    }

    public function accessToken()
    {
        $_data = $this->wxHttpsRequestPem('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::APPID.'&secret='.self::APPSECRET);
        $_data = json_decode($_data,true);
        return $_data['access_token'];
    }

    public function ticket()
    {
        $_data = $this->wxHttpsRequestPem('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->accessToken().'&type=jsapi');
        $_data = json_decode($_data,true);
        return $_data['ticket'];
    }

    /**
     * 获取sign
     * @param unknown $_data
     * @return string
     */
    private function sign($_data){
        ksort($_data);
        $_stringA = '';
        foreach($_data as $_key=>$_value){
            $_stringA .= $_key.'='.$_value.'&';
        }
        $_string = substr($_stringA,0,-1);
        return sha1($_string);
    }

    /**
     *
     * @param unknown $url
     * @param unknown $vars
     * @param string $_cert  //是否带上证书
     * @param number $second
     * @param array $aHeader
     * @return mixed|boolean
     */
    public function wxHttpsRequestPem($url, $vars = '',$_cert = false, $second=30,$aHeader=array()){
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        if($_cert){
            //以下两种方式需选择一种
            //第一种方法，cert 与 key 分别属于两个.pem文件
            //默认格式为PEM，可以注释
            curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
            curl_setopt($ch,CURLOPT_SSLCERT,$this->_cert_cert);
            //默认格式为PEM，可以注释
            curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
            curl_setopt($ch,CURLOPT_SSLKEY,$this->_cert_key);
            curl_setopt($ch,CURLOPT_CAINFO,'PEM');
            //($ch,CURLOPT_CAINFO,ATTACHMENT_ROOT.$this->_ca);
            //第二种方式，两个文件合成一个.pem文件
            //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
        }
        if(count($aHeader) >= 1 ){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        if(!empty($vars)){
            curl_setopt($ch,CURLOPT_POST, 1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
        }
        $data = curl_exec($ch);
        if($data){
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }
}
